﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace myPhoneBook
{
    public partial class Form1 : Form
    {
        DataSetMyPhoneBook ds = new DataSetMyPhoneBook();
        DataSetMyPhoneBookTableAdapters.ContactsTableAdapter contact_adt = new DataSetMyPhoneBookTableAdapters.ContactsTableAdapter();
        DataSetMyPhoneBookTableAdapters.HistoriesTableAdapter history_adt = new DataSetMyPhoneBookTableAdapters.HistoriesTableAdapter();
        public Form1()
        {
            InitializeComponent();
            //Buoc 1 Fill
            contact_adt.Fill(ds.Contacts);
            //history_adt.Fill(ds.Histories);

            //Buoc 2 hien thi
            dataGridViewMain.DataSource = ds.Contacts;

            //Cac cai add, find...
            //find tra ve ContactsRow
            //v phai go dung ten bang roi Row thi no co goi y de using namespace nhu vua roi
            //myPhoneBook.DataSetMyPhoneBook.ContactsRow i = ds.Contacts.FindByCID(truyenidvao);
            //neu tra ve null -> khong tim thay Row co id truyen vao
            
            
            //add
            //ds.Contacts.AddContactsRow(vo chon 1 trong 2 overload);
            

            //tat ca xong thi phai cho adapter update ve CSDL
            //contact_adt.Update(ds.Contacts);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormAdd frmAdd = new FormAdd(ds, contact_adt, history_adt);
            frmAdd.Show();
        }

        private void searchByName(object sender, EventArgs e)
        {
            contact_adt.Fill(ds.Contacts);
            foreach (myPhoneBook.DataSetMyPhoneBook.ContactsRow i in ds.Contacts.Rows)
            {
                if (!i.ContactName.ToLower().Contains(txtKeyword.Text.ToLower()))
                {
                    i.Delete();
                }
            }
        }

        private void showDetail(object sender, DataGridViewCellEventArgs e)
        {
            int id = Convert.ToInt32(dataGridViewMain.SelectedRows[0].Cells["CID"].Value.ToString());
        }
    }
}
